package functions.ems_base_data;

import java.util.ArrayList;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

import com.beans.CityBean;


import coms.Constants;
import frame.HttpHelper;

/**
 * 城市管理
 * @author CHENHAILIAN6
 *
 */
public class City {
	private org.slf4j.Logger logger = LoggerFactory.getLogger(City.class);
	
	String result;
	
	ArrayList<CityBean> cities;
	CityBean startCity;
	CityBean desCity;
	
	public CityBean getStartCity() {
		return startCity;
	}


	public void setStartCity(CityBean startCity) {
		this.startCity = startCity;
	}


	public CityBean getDesCity() {
		return desCity;
	}


	public void setDesCity(CityBean endCity) {
		this.desCity = endCity;
	}


	

	/**
	 * 查询出差城市列表
	 * findCityListForTripUrl="/ems-base-data/city/findCityListForTrip"
	 */
	public void findCityListForTrip() {
		logger.info("查询出差城市列表 ----------------- start ------------");
		String url = Constants.FIND_CITY_LIST_FOR_TRIP_URL;
		try {
			result = HttpHelper.post("", url);
			logger.info("VEHICLE --- " + result);
			try {
				JSONObject datas = new JSONObject(result);
				JSONArray dataResult = datas.getJSONArray("allCityList");

				cities = new ArrayList<CityBean>();
				for (int i = 0; i < dataResult.length(); i++) {
					JSONObject json = (JSONObject) dataResult.get(i);
					try {
						String cityId = String.valueOf(json.getInt("cityId"));
						String province = json.getString("province");
						String cityName = json.getString("cityName");
						String cityType = json.getString("cityType");
						String cityFirstName = json.getString("cityFirstName");
						String cityAbbreviation = json.getString("cityAbbreviation");
						String isHot = json.getString("isHot");
						String fullCityName = json.getString("fullCityName");
						String isHotDisplay = json.getString("isHotDisplay");
						String cityTypeDisplay = json.getString("cityTypeDisplay");
						CityBean cityBean = new CityBean(cityId, province, cityName, cityType, cityFirstName, cityAbbreviation,
								isHot, fullCityName, isHotDisplay, cityTypeDisplay);
						cities.add(cityBean);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int size = cities.size();
		// System.out.println(size);
		if (size > 0) {
			Random dom = new Random(size);
			int i = dom.nextInt(size - 1);
			// System.out.println(size+ "rom: " + i);
			startCity = cities.get(i);
			logger.info("selected start city --- " + startCity.toString());
			i = dom.nextInt(size - 1);
			// System.out.println(size+ "rom: " + i);
			desCity = cities.get(i);
			logger.info("selected end city --- " + desCity.toString());
		}
		
		logger.info("查询出差城市列表 ----------------- end ------------");
	}
}
